//
//  TTOAutoViewController.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/20.
//  Copyright © 2016年 陶路路. All rights reserved.
//

import UIKit

class TTOAutoViewController: UIViewController {

    
//    懒加载 WebView 
    
    lazy var webView : UIWebView = UIWebView()
    
    lazy var progressView : TTOAutoProgress = TTOAutoProgress()
    
    override func loadView() {
        
        view = webView
        webView.delegate = self
        
       
    }
    
//    设置WebView 为控制器view
    
    override func viewDidLoad() {
        
        super.viewDidLoad()

        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(AppKey)&redirect_uri=\(APPREDIRECT_URI)"
        
        let url = NSURL(string: urlString)
        
        if let u = url {
            
            let request = NSURLRequest(URL: u)
            
            webView.loadRequest(request)
        }
        
        setUpUI()
        
    }
    
//    左右侧按钮
    
    func setUpUI(){
        
         navigationController?.view.insertSubview(progressView, belowSubview: (navigationController?.navigationBar)!)
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", imag: nil, target: self, action: "cancelButton")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "填充", imag: nil, target: self, action: "fillButton")
    }
    
    func fillButton(){
        
//   拼接 JavaScript 字符串
        
        let jsString = "document.getElementById('userId').value='\(wbName)',document.getElementById('passwd').value='\(wbPasswd)'"
        
//   webView 执行JavaScript 语言
        
        webView.stringByEvaluatingJavaScriptFromString(jsString)
    }
    
    func cancelButton(){
        
         dismissViewControllerAnimated(true, completion: nil)
    }
    

    override func viewDidDisappear(animated: Bool) {
        
        progressView.remove()
    }
   
    deinit{
        
    }
}


extension TTOAutoViewController:UIWebViewDelegate{
    
    func webViewDidStartLoad(webView: UIWebView) {
        
        progressView.starAnima()
        
    }
    
    
    func webViewDidFinishLoad(webView: UIWebView) {
        
        progressView.endAnima()
    }
    
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
//        获取请求的url字符串
        let urlString = request.URL?.absoluteString
//        获取url前缀
        if let u = urlString where u.hasPrefix(APPREDIRECT_URI){
//        获取请求参数
            let query = request.URL?.query
            
            if let q = query{
//        分割字符串 获取参数
               
                let code = q.substringFromIndex("code=".endIndex)
                
                TTOAutoModel.sharedOautoModel.getUserData(code, isSuccess: { (succsee) -> () in
                    
                    if !succsee {
                        
                        return
                    }
                    
                    self.dismissViewControllerAnimated(false, completion: { () -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName("TTWecomeController", object: nil)
                        
                        
                    })
                  
                })
                return false
            }
        }
        return true
    }
}
